package com.lym.car.controller;

import com.car.pojo.carRoad;
import com.car.util.AesEncryptUtil;
import com.car.util.JwtUtil;
import com.car.util.Result;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RequestMapping("roadclient")
@RestController
public class RoadClientController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private JwtUtil jwtUtil;

    @RequestMapping(value = "getroad",method = RequestMethod.GET)
//    @HystrixCommand(fallbackMethod = "findBacks")
    public Object getAppKey(String appKey) throws Exception {
//        String key = "dufy20170329java";
//        String iv = "dufy20170329java";
//        //截取？号前数据
//        String str1=appKey.substring(0, appKey.indexOf("?"));
//        //截取？号后数据
//        String str2=appKey.substring(str1.length(), appKey.length());
//        String encrypt = AesEncryptUtil.desEncrypt(str1,key,iv);
//        //拼接解密后数据
//        String ids = encrypt+str2;
        String token = request.getParameter("token");
        try {
            Claims claims = jwtUtil.parseJWT(token);
        }catch (Exception e){
            return new Result(false,"token有误！请联系管理员");
        }
        List result = restTemplate.getForObject("http://car-system/road/"+appKey, List.class);
        return result;
    }
    @RequestMapping(value = "getroadRSA",method = RequestMethod.POST)
//    @HystrixCommand(fallbackMethod = "findBacks")
    public Object getroadRSA(String appKey, @RequestBody carRoad carRoads){
        String token = request.getParameter("token");
        try {
            Claims claims = jwtUtil.parseJWT(token);
        }catch (Exception e){
            return new Result(false,"token有误！请联系管理员");
        }
        Result result = restTemplate.postForObject("http://car-system/road/res",carRoads,Result.class);
        return result;
    }

    @RequestMapping(value = "getroadinsert",method = RequestMethod.POST)
//    @HystrixCommand(fallbackMethod = "findBacks")
    public Object getroadinsert(String appKey, @RequestBody List<carRoad> carRoads){
        String token = request.getParameter("token");
        try {
            Claims claims = jwtUtil.parseJWT(token);
        }catch (Exception e){
            return new Result(false,"token有误！请联系管理员");
        }
        Result result = restTemplate.postForObject("http://car-system/road/batchInsert",carRoads,Result.class);
        return result;
    }
    @RequestMapping(value = "getadmin",method = RequestMethod.GET)
//    @HystrixCommand(fallbackMethod = "findBacks")
    public Object getadmin(String appKey) throws Exception {

        String key = "dufy20170329java";
        String iv = "dufy20170329java";
        //截取？号前数据
        String ids = null;
        if(!appKey.contains("?")){
            String encrypt = AesEncryptUtil.desEncrypt(appKey,key,iv);
            ids = encrypt;
        }else{
            String str1=appKey.substring(0, appKey.indexOf("?"));
            //截取？号后数据
            String str2=appKey.substring(str1.length(), appKey.length());
            String encrypt = AesEncryptUtil.desEncrypt(str1,key,iv);
            //拼接解密后数据
            ids =  encrypt+str2;
        }
        String token = request.getParameter("token");
        try {
            Claims claims = jwtUtil.parseJWT(token);
        }catch (Exception e){
            return new Result(false,"token有误！请联系管理员");
        }
        String result = restTemplate.getForObject("http://car-system/road/"+ids, String.class);
        return result;
    }
    public Result findBacks(String appKey, HttpServletRequest request){
        return new Result(false,"服务中断。。。请重试！");
    }



}
